package com.blackline.UI.Dashboards;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.blackline.auto.NewDashboard.NewDashboard_smoketest;
import com.blackline.auto.common.CommonMethods;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>This page is after clicking REPORTS > Dashboards from navigation menu.
 * Please refer to UI.Dashboards.png in doc-files.<br>
 */
public class Dashboards {
	private static Logger logger=Logger.getLogger(Dashboards.class);
	
	/**
	 * User Name at the top right corner
	 */
	@FindBy (id = "uname")
	public WebElement UserName;
	
	/**
	 * Period at the top right corner
	 */
	@FindBy (xpath = "//div[@id='nav-periods']/header/div/span")
	public WebElement Period;
	
	/**
	 * Help link
	 */
	@FindBy (id = "hTab")
	public WebElement Help;
	
	/**
	 * Log out
	 */
	@FindBy (id = "logout")
	public WebElement Log_Out;
	
	
	
	/**
	 * Add Widget link
	 */
	@FindBy(id = "add-widget")
	public WebElement Add_Widget_link;
	
	/**
	 * Add button in the pop-up window after clicking Add Widget link
	 */
	@FindBy (id = "btn-add-widget")
	public WebElement Add_button;
	
	
	/**
	 * "Exit the dashboard preview" link
	 */
	@FindBy (id = "preview")
	public WebElement Exit_the_dashboard_preview_link;
	
	/**
	 * Accounts menu
	 */
	@FindBy(xpath = "//a/span[@title='Accounts']")
	public WebElement Accounts_menu;
	
	
	/**
	 * Match menu
	 */
	@FindBy(xpath = "//a/span[@title='Match']")
	public WebElement Match_menu;
	
	
	/**
	 * Journals menu
	 */
	@FindBy(xpath = "//a/span[@title='Journals']")
	public WebElement Journals_menu;
	

	
	/**
	 * Tasks menu
	 */
	@FindBy(xpath = "//a/span[@title='Tasks']")
	public WebElement Tasks_menu;
	
	
	
	/**
	 * CIM menu
	 */
	@FindBy(xpath = "//a/span[@title='CIM']")
	public WebElement CIM_menu;
	
	
	
	/**
	 * Variance menu
	 */
	@FindBy(xpath = "//a/span[@title='Variance']")
	public WebElement Variance_menu;
	
	
	
	/**
	 * Reports menu
	 */
	@FindBy(xpath = "//a/span[@title='Reports']")
	public WebElement Reports_menu;
	
	
	
	/**
	 * Requests menu
	 */
	@FindBy(xpath = "//a/span[@title='Requests']")
	public WebElement Requests_menu;
	
	
	
	/**
	 * Tools menu
	 */
	@FindBy(xpath = "//a/span[@title='Tools']")
	public WebElement Tools_menu;
	
	//------------------------------------------//
	// Options under Accounts menu
    //------------------------------------------//
	/**
	 * Accounts > Accounts
	 */
	@FindBy (xpath = "//li/a[@title='Accounts']")
	public WebElement Accounts_option;
	
	/**
	 * Accounts > Mass Updates
	 */
	@FindBy (xpath = "//li/a[@title='Mass Updates']")
	public WebElement Mass_Updates_option;
	
	/**
	 * Accounts > Mass Updates > Mass Update Due Date Settings
	 */
	@FindBy (xpath = "//li/a[@title='Mass Update Due Date Settings']")
	public WebElement Mass_Update_Due_Date_Settings_option;
	
	/**
	 * Accounts > Mass Updates > Bulk Assignment - Accounts
	 */
	@FindBy (xpath = "//li/a[@title='Bulk Assignment - Accounts']")
	public WebElement Bulk_Assignment_Accounts_option;
	
	/**
	 * Accounts > Mass Updates > Reset Temporary Assignments - Accounts
	 */
	@FindBy (xpath = "//li/a[@title='Reset Temporary Assignments - Accounts']")
	public WebElement Reset_Temporary_Assignments_Accounts_option;
	
	/**
	 * Accounts > Mass Updates > Bulk Assignment - Management Review Groups
	 */
	@FindBy (xpath = "//li/a[@title='Bulk Assignment - Management Review Groups']")
	public WebElement Bulk_Assignment_Management_Review_Groups_option;
	
	/**
	 * Accounts > Mass Updates > Reset Temporary Assignments - Management Review Groups
	 */
	@FindBy (xpath = "//li/a[@title='Reset Temporary Assignments - Management Review Groups']")
	public WebElement Reset_Temporary_Assignments_Management_Review_Groups_option;
	
	/**
	 * Accounts > Mass Updates > Mass Update Account Settings
	 */
	@FindBy (xpath = "//li/a[@title='Mass Update Account Settings']")
	public WebElement Mass_Update_Account_Settings_option;
	
	/**
	 * Accounts > Mass Updates > Mass Update Management Review Group Settings
	 */
	@FindBy (xpath = "//li/a[@title='Mass Update Management Review Group Settings']")
	public WebElement Mass_Update_Management_Review_Group_Settings_option;
	
	/**
	 * Accounts > View Reconciliations
	 */
	@FindBy (xpath = "//li/a[@title='View Reconciliations']")
	public WebElement View_Reconciliations_option;
	
	
	//------------------------------------------//
	// Options under Match menu
    //------------------------------------------//
	
	//------------------------------------------//
	// Options under Journals menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under Tasks menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under CIM menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under Variance menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under Reports menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under Requests menu
	//------------------------------------------//
	
	//------------------------------------------//
	// Options under Tools menu
	//------------------------------------------//
	
	
	/**
	 * Restore Default Layout link
	 */
	@FindBy (id = "restore-layout")
	public WebElement Restore_Default_Layout_link;
	
	/**
	 * Check how many widgets exist in this page.
	 * @param driver The WebDriver used in test.
	 * @return A int. How many widgets exists in the page.
	 */
	public int numberOfWidgets(WebDriver driver)
	{
		String pageContents = driver.getPageSource();
		String txtToFind = "<div class=\"chart-wrapper\" id=\"chartWrapper";
		String tempFilePath = "testdata\\pagesource.txt";
		CommonMethods cm = new CommonMethods();
		cm.createFile(tempFilePath, pageContents);
		int number = numberOfSpecificString(tempFilePath,txtToFind); 
		cm.deleteFile(tempFilePath);
		return number;
	}
	
	/**
	 * How many specific string can be found in a file
	 * @param filePath The path of the file to read: c:\\a.txt
	 * @param textToFind The text to be found in the file
	 * @return The number of the specific string found in the file. <br>
	 * Sample:<br>
	 * 
	 * 
	 */
	private int numberOfSpecificString(String filePath, String textToFind) {
		File file = new File(filePath);
		List<String> list = new ArrayList<String>();
		int numberOfWidgets = 0;
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line = null;

			// Store data into list
			while ((line = br.readLine()) != null) {
				list.add(line);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < list.size(); i++) {
			String s = (String) list.get(i);
			if (s.contains(textToFind))
			{
				numberOfWidgets++;
			}
		}
		logger.info("Number of widget(s) should be: " + numberOfWidgets);
		return numberOfWidgets;
	}
	

}
